package com.yang.service.impl;

import com.yang.dao.CheckInDao;
import com.yang.dao.DataStatisticeDao;
import com.yang.dao.SelectInformationDao;
import com.yang.entity.GuestEntity;
import com.yang.entity.LiverRelationEntity;
import com.yang.entity.OrderFormEntity;
import com.yang.entity.RoomEntity;
import com.yang.pojo.GuestDto;
import com.yang.pojo.ModificationDto;
import com.yang.service.SelectInfomationService;
import com.yang.util.PublicUtil;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SelectInfomationServiceImpl implements SelectInfomationService {
    private static Logger logger = Logger.getLogger(SelectInfomationServiceImpl.class);

    @Resource
    SelectInformationDao selectInformationDao;

    @Resource
    DataStatisticeDao dataStatisticeDao;

    @Resource
    CheckInDao checkInDao;

    @Override
    public Map<String, Object> selectInformation(String selectwhich, String guestStatus, String selectName) {
        Map<String,Object> map = new HashMap<String, Object>();
        List<GuestEntity> guestEntities = null;
        List<GuestDto> guestDtos = new ArrayList<GuestDto>();
        //以姓名查找
        if(selectwhich.equals("1")){

            //获取顾客表
            guestEntities = selectInformationDao.selectGuestByNam(selectName);
            if(guestEntities == null || guestEntities.size() == 0){
                logger.error("顾客信息为空");
                map.put("status","error");
                map.put("info","未找到任何信息");
                return map;
            }

            for(GuestEntity guestEntity : guestEntities){

                //获取liver关系    顾客ID+状态可以找到一个关系表
                LiverRelationEntity liverRelationEntity = selectInformationDao.selectLiverByguesIdAndStatus(guestEntity.getId(),guestStatus);
                if(liverRelationEntity == null){
                    logger.error("liver表查找不到  "+guestEntity.toString());
                    map.put("status","error");
                    map.put("info","未找到任何信息");
                    return map;
                }

                //获取房号表
                RoomEntity roomEntity = dataStatisticeDao.selectRoomById(liverRelationEntity.getRoomId());
                if(roomEntity==null){
                    logger.error("找不到房间表"+liverRelationEntity.toString());
                    map.put("status","error");
                    map.put("info","未找到任何信息");
                    return map;
                }

                //获取订单表  一个关系表可以找到一个orderID
                OrderFormEntity orderFormEntity = selectInformationDao.selectOrderByOrderId(liverRelationEntity.getOrderFormId());
                if(orderFormEntity==null){
                    logger.error("找不到order表"+roomEntity.toString());
                    map.put("status","error");
                    map.put("info","未找到任何信息");
                    return map;
                }

                String deadline = PublicUtil.countDeadline(orderFormEntity.getBeginDay(),orderFormEntity.getStayDay());

                GuestDto guestDto = new GuestDto();
                guestDto.setName(guestEntity.getName());
                guestDto.setIdentity(guestEntity.getIdetifcation());
                guestDto.setRoomNum(roomEntity.getRoomNum());
                guestDto.setDeadline(deadline);
                logger.info(guestDto.toString());
                guestDtos.add(guestDto);
            }
            if(guestDtos.size() == 0){
                map.put("status","error");
                map.put("info","未找到任何信息！");
            }else{
                map.put("status","succeed");
                map.put("info",guestDtos);
            }
        }else{
            //找到room表
            RoomEntity roomEntity = selectInformationDao.selectRoomByRoomNum(selectName);
            if(roomEntity == null){
                logger.error("找不到roomEnty表"+selectName);
                map.put("status","error");
                map.put("info","房号出错");
                return map;
            }

            //找到关系表
            List<LiverRelationEntity> liverRelationEntities = selectInformationDao.selectLiveByRoomIdAndStatus(roomEntity.getId(),guestStatus);
            if(liverRelationEntities==null || liverRelationEntities.size() == 0){
                logger.error("找不到liverRelationEntities表"+roomEntity.toString());
                map.put("status","error");
                map.put("info","未找到任何信息");
                return map;
            }

            for(LiverRelationEntity liverRelationEntity : liverRelationEntities){
                GuestEntity guestEntity = selectInformationDao.selectGuestByGuestId(liverRelationEntity.getGuestId());
                if(guestEntity==null){
                    logger.error("找不到guestEntity表"+liverRelationEntity.toString());
                    map.put("status","error");
                    map.put("info","未找到任何信息");
                    return map;
                }
                OrderFormEntity orderFormEntity = selectInformationDao.selectOrderByOrderId(liverRelationEntity.getOrderFormId());
                if(orderFormEntity==null){
                    logger.error("找不到orderFormEntity表"+liverRelationEntity.toString());
                    map.put("status","error");
                    map.put("info","未找到任何信息");
                    return map;
                }
                String deadline = PublicUtil.countDeadline(orderFormEntity.getBeginDay(),orderFormEntity.getStayDay());
                GuestDto guestDto = new GuestDto();
                guestDto.setName(guestEntity.getName());
                guestDto.setRoomNum(selectName);
                guestDto.setIdentity(guestEntity.getIdetifcation());
                guestDto.setDeadline(deadline);
                logger.info(guestDto.toString());
                guestDtos.add(guestDto);
            }
            if(guestDtos.size() == 0){
                map.put("status","error");
                map.put("info","未找到任何信息！");
            }else{
                map.put("status","succeed");
                map.put("info",guestDtos);
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> selectModification(String roomNum, String name) {
        Map<String,Object> map = new HashMap<String, Object>();
        List<ModificationDto> modificationDtos = null;
        try{
            modificationDtos = selectInformationDao.selectModByRoomAndName(roomNum,name);
        }catch (Exception e){
            map.put("status","error");
            map.put("info","找不到任何信息");
            logger.error("modificationDts 查找异常");
            return map;
        }
        if(modificationDtos == null || modificationDtos.size() == 0){
            map.put("status","error");
            map.put("info","找不到任何信息");
            logger.error("找不到任何信息");
            return map;
        }
        map.put("status","succeed");
        map.put("info",modificationDtos);
        return map;
    }

    @Override
    public Map<String, Object> Modification(long id, String name, String tel, String identification) {
        Map<String ,Object> map = new HashMap<String, Object>();
        GuestEntity guestEntity = selectInformationDao.selectGuestByGuestId(id);
        if(guestEntity == null){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("通过id找不到GuestEntity  id = "+id);
            return map;
        }
        guestEntity.setName(name);
        guestEntity.setTel(tel);
        guestEntity.setIdetifcation(identification);
        if(!selectInformationDao.updataGuest(guestEntity)){
            map.put("status","error");
            map.put("info","网络出错");
            logger.error("更新guestEntity出错   "+guestEntity);
            return map;
        }
        map.put("status","succeed");
        map.put("info","修改成功");
        map.put("content",guestEntity);
        logger.info("修改成功 GuestEntity = "+guestEntity);
        return map;
    }

}
